//
//  DepartSearchViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/11.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class DepartSearchViewController: ConditionSelectViewController {

    
    //日期为必选
    let picker = YearMonthPicker.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 220)))
    override func viewDidLoad() {
        self.ifdepart = true
        super.viewDidLoad()
        self.yearField.inputView = picker
    }
    
    override func endedit() {
        super.endedit()
        if let cell = self.tableView.cellForRow(at: IndexPath(row: 1, section: 0)) {
            cell.detailTextLabel?.text = picker.date
        }
    }
    
    override func canceledit() {
        super.canceledit()
    }
    
    var date: String? {
        set{
            picker.date = date
        }
        get{
            return picker.date
        }
    }
    
    override func animator()->Void{
        let animation = POPBasicAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.fromValue = NSValue.init(cgRect: self.tableView.frame)
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize(width: self.view.width, height: 165)))
        animation?.duration = TimeInterval.init(0.1)
        self.tableView.pop_add(animation, forKey: "t")
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let targetitem = self.companys.filter { (json) -> Bool in
                return json.contents?["code"]?.rawString() == self.selectedOrg
            }
            cell.textLabel?.text = "部门:"
            if targetitem.count > 0 {
                let item = targetitem[0]
                cell.detailTextLabel?.text = item.contents?["name"]?.rawString()
            }
        } else if indexPath.row == 1 {
            cell.textLabel?.text = "年月:"
            if self.date != nil {
                cell.detailTextLabel?.text = self.date
            }
        }
    }
    
    @objc override func searchAction()->Void{
        self.view.endEditing(true)
        self.delegate?.search(with: self.selectedOrg, yearmonth: self.picker.date, projname: nil)
        self.dismiss()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
